part of 'alert.dart';

///选择相对通道
class AlertSelectChannel extends StatelessWidget {
  final void Function(int i)? onPressed;
  final int groupIndex;
  final ChConfig chConfig;
  const AlertSelectChannel(
      {Key? key,
      this.onPressed,
      required this.groupIndex,
      required this.chConfig})
      : super(key: key);

  Color get lineColor => Colors.black.withOpacity(0.2);

  @override
  Widget build(BuildContext context) {
    return UnconstrainedBox(
        child: Material(
      color: Colors.transparent,
      child: Container(
        decoration: kAlertBoxDecoration,
        clipBehavior: Clip.hardEdge,
        height: 207,
        width: 304,
        child: Column(
          children: [
            Container(
              alignment: Alignment.center,
              height: 50,
              child: Text(
                'Select Benchmark Channel',
                style: TextStyle(fontSize: 20, color: Colors.white),
              ),
            ),
            ...List.generate(
                4, (index) => Flexible(child: _buildRow(index, context))),
          ],
        ),
      ),
    ));
  }

  Widget _buildRow(int row, BuildContext context) {
    return Container(
      decoration: BoxDecoration(
        border: row == 3 ? null : Border(bottom: BorderSide(color: lineColor)),
        color: Colors.white,
      ),
      alignment: Alignment.center,
      child: Row(children: [
        Flexible(child: _buildCh(row * 2, context)),
        VerticalDivider(
          width: 1,
          color: lineColor,
        ),
        Flexible(child: _buildCh(row * 2 + 1, context)),
      ]),
    );
  }

  Widget _buildCh(int ch, BuildContext context) {
    var color = Colors.black;
    var enable = true;
    if (chConfig.chs[ch].groupIndex == groupIndex) {
      //同组
      color = chConfig.chs[ch].relativeCh == ch
          ? Theme.of(context).primaryColor
          : Colors.black;
    } else {
      color = Colors.grey;
      enable = false;
    }
    return GestureDetector(
      onTap: () {
        if (!enable) return;
        onPressed?.call(ch);
        Get.back();
      },
      child: Container(
        // color: Colors.red,
        child: Text(
          DataMng.shared.chNames[ch].value,
          style: TextStyle(color: color),
        ),
        alignment: Alignment.center,
      ),
    );
  }
}
